package com.qianhui.dao;

import com.qianhui.common.IResult;
import com.qianhui.pojo.Shop;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for Shop
 * entities. Transaction control of the save(), update() and delete() operations
 * can directly support Spring container-managed transactions or they can be
 * augmented to handle user-managed Spring transactions. Each of these methods
 * provides additional information for how to configure it for the desired type
 * of transaction control.
 *
 * @author MyEclipse Persistence Tools
 * @see com.qianhui.pojo.Shop
 */
public class ShopDAO extends BaseHibernateDAO
{
    private static final Logger log = LoggerFactory.getLogger(ShopDAO.class);
    // property constants
    public static final String QIANHUI_MIN_LIMIT = "qianhuiMinLimit";
    public static final String MZC_MIN_NUMBER = "mzcMinNumber";

    public Shop save(Shop transientInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return transientInstance;
    }

    public int delete(Shop persistentInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public Shop findById(java.lang.String id)
    {
        log.debug("getting Shop instance with id: " + id);
        try
        {
            Shop instance = (Shop) getSession().get("com.qianhui.pojo.Shop", id);
            return instance;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding Shop instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from Shop as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByQianhuiMinLimit(Object qianhuiMinLimit)
    {
        return findByProperty(QIANHUI_MIN_LIMIT, qianhuiMinLimit);
    }

    public List findByMzcMinNumber(Object mzcMinNumber)
    {
        return findByProperty(MZC_MIN_NUMBER, mzcMinNumber);
    }

    public List findAll()
    {
        log.debug("finding all Shop instances");
        try
        {
            String queryString = "from Shop";
            Query queryObject = getSession().createQuery(queryString);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

}